package com.lee.xcore.base

import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.viewbinding.ViewBinding
import com.lee.xcore.eventbus.Event

/**
 *
 */
abstract class BaseVMActivity<VB : ViewBinding, VM : ViewModel> :AppCompatActivity(),IBasisView{
    private lateinit var _vb: VB
    private lateinit var _vm: VM
    protected val V get() = _vb
    protected val M get() = _vm
    lateinit var mContext: AppCompatActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _vb = getViewBinding()
        _vm = getViewModel()
        if (isEventBusEnable()) {
            Event.getInstance().register(this)
        }
        beforeSetContentView()
        setContentView(_vb.root)
        mContext = this
        beforeInitView(savedInstanceState)
        initStateBar()
        initView(savedInstanceState)
        createEvent()
        loadData()
        createObserver()
    }

    override fun onDestroy() {
        if (isEventBusEnable()) {
            Event.getInstance().unregister(this)
        }
        super.onDestroy()
    }

    protected abstract fun getViewBinding(): VB
    protected abstract fun getViewModel(): VM

    private var isExit: Boolean = false

    fun quickExit(tipsAction: () -> Unit,exitAction: () -> Unit) {
        val handler = Handler()
        if ((!isExit)) {
            isExit = true
            tipsAction()
            handler.postDelayed({ isExit = false }, 1000 * 2) //x秒后没按就取消
        } else {
            exitAction()
        }
    }
}